<?php

/**
 *  ajax post comment
 */
function crabii_app_post_comment() {
  CommonHelper::check_token('crabii_app');

  if(empty($_POST) && !isset($_POST['comment_body'])) {
    drupal_access_denied();
  }

  global $user;
  $comment_body = $_POST['comment_body'];
  $nid = $_POST['nid'];
  $view_mode = $_POST['view_mode'];

  $comment = (object) array(
    'nid' => $nid,
    'cid' => 0,
    'pid' => 0,
    'uid' => $user->uid,
    'is_anonymous' => 0,
    'homepage' => '',
    'status' => COMMENT_PUBLISHED,
    'subject' => '',
    'language' => LANGUAGE_NONE,
    'comment_body' => array(
      LANGUAGE_NONE => array(
        0 => array (
          'value' => $comment_body,
          'format' => 'plain_text'
        )
      )
    ),
  );

  comment_submit($comment);
  comment_save($comment);

  $comment->picture = $user->picture; // need to load user picture

  $node = new stdClass();
  $node->nid = $nid;
  $node->type = NIP_TYPE;
  node_object_prepare($node);

  $build = comment_view($comment, $node, $view_mode);

  drupal_json_output(render($build));
}

/**
 * output reader
 */
function crabii_app_reader() {
  CommonHelper::check_token('crabii_app');

  if(empty($_POST)) {
    drupal_access_denied();
    drupal_exit();
  }

  if(!isset($_POST['url']) || empty($_POST['url'])) {
    drupal_access_denied();
    drupal_exit();
  }

  CommonHelper::reader($_POST['url']);
}

/**
 * 404 page
 */
function crabii_error_404() {
  return array(
    '#theme' => 'crabii_error',
    '#error' => 404,
    '#exclamation' => t('Opsss...'),
    '#description' => t('The page you are looking for doesn\'t exist'),
    '#attached' => array('css' => array(drupal_get_path('module', 'crabii_app') . '/css/error.css')),
  );
}

/**
 * 403 page
 */
function crabii_error_403() {
  return array(
    '#theme' => 'crabii_error',
    '#error' => 403,
    '#exclamation' => t('Grrr..r..r....'),
    '#description' => t('You do not have permission for this request'),
    '#attached' => array('css' => array(drupal_get_path('module', 'crabii_app') . '/css/error.css')),
  );
}
